<?php

require_once '../application/modules/admin/controllers/SuperControllerAdmin.php';

class Admin_AdvController extends Admin_SuperController {

    public function indexAction() {
        if ($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout->disableLayout();
            if ($this->_request->event == 'removeRow')
                $this->onRemoweRow($this->_request->id);
            if ($this->_request->event == 'activateRow')
                $this->onActivateRow($this->_request->id);
        }

        $this->view->order_field = isset($this->_request->order_field) ? $this->_request->order_field : 'id';
        $this->view->order_dir = isset($this->_request->order_dir) ? $this->_request->order_dir : 'DESC';

        $this->view->tableColumn = array();
        $this->view->tableColumn[0]['title'] = 'id';
        $this->view->tableColumn[1]['title'] = 'name';
        $this->view->tableColumn[2]['title'] = 'email';

        $this->view->tableColumn[3]['title'] = 'title';
        $this->view->tableColumn[3]['wrap'] = 'div150';
        $this->view->tableColumn[4]['title'] = 'region';
        $this->view->tableColumn[4]['wrap'] = 'div150';
        $this->view->tableColumn[5]['title'] = 'active';
        $this->view->tableColumn[6]['title'] = 'Change';
        
        $view_page = new Zend_Session_Namespace('view_apage');
        $onPage = $view_page->on_page;
        //$onPage = 4;
        $page = (int) $this->_request->page;
        if (empty($page))
            $page = 1;

        $qAr = array(
            'table_name' => 'applicants',
            'order_field' => $this->view->order_field,
            'order_dir' => $this->view->order_dir,
            'offset' => ($page - 1) * $onPage,
            'limit' => $onPage,
        );
        
        $ADVM = new Application_Model_Adv();
        $order_by = 'a.`id` DESC';
        $search = '';
        $search = (isset($_GET['search'])) ? $_GET['search'] : '';
        $where = '';
        if ($this->_request->user) // user adv
            $where = ' AND a.`user_id` = ' . (int) $this->_request->user;
        $result = $ADVM->getAdv($page, $onPage, $order_by, $search, $where, TRUE);

        $this->view->info = $result['result'];
        //_debug($this->view->info);
        $this->view->countRow = $result['total'];

        $paginator = Zend_Paginator::factory((int) $this->view->countRow);
        $paginator->setDefaultItemCountPerPage($onPage);
        $paginator->setCurrentPageNumber($page);

        $this->view->paginator = $paginator;
        $this->view->onPage = $onPage;
    }

    protected function onRemoweRow($id) {
        // делаем не активным
        if (!empty($id)) {
            $ADVM = new Application_Model_Adv();
            return $ADVM->setNoactiveAdv($id, 0);
        }
        return false;
    }

    protected function onActivateRow($id) {
        // делаем не активным
        if (!empty($id)) {
            $ADVM = new Application_Model_Adv();
            return $ADVM->setNoactiveAdv($id, 1);
        }
        return false;
    }
}
